package com.example.easypoi.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.example.easypoi.mapper.MaintainMapper;
import com.example.easypoi.model.Maintain;
import com.example.easypoi.model.MaintainDto;
import com.example.easypoi.model.MaintainDtoList;
import com.example.easypoi.service.MaintainService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 朱超
 * @createDate: 2023/3/28
 * @version: 1.0
 * @remark：备注
 */
@Service
public class MaintainServiceImpl implements MaintainService {
    @Resource
    private MaintainMapper maintainMapper;

    @Autowired(required = false)
    private HttpServletResponse response;

    @Autowired(required = false)
    private IExcelVerifyHandler excelVerifyHandler;
//    @Override
//    public Integer addMaintain(Maintain maintain) {
//        return null;
//    }

    @Override
    public Map<String, Object> addMaintain(MultipartFile file) throws Exception {
        Map<String, Object> map = new HashMap<>();
        //导入的基本配置
        ImportParams params = new ImportParams();
        //表头一行
        params.setHeadRows(1);
        //标题一行
        params.setTitleRows(1);
        //代表导入这里是需要验证的（根据字段上的注解）
//        params.setNeedVerify(true);
        //设及一个自定义校验 （自定义校验名字不可重复）
        params.setVerifyHandler(excelVerifyHandler);
        //使用框架自身导入工具
        ExcelImportResult<Maintain> result = ExcelImportUtil.importExcelMore(file.getInputStream(), Maintain.class, params);
        //导入成功的数据
        List<Maintain> list = result.getList();
        //失败结果集
        List<Maintain> failList = result.getFailList();

        //拿到导出失败的工作簿
        Workbook failWorkbook = result.getFailWorkbook();
        failWorkbook.write(response.getOutputStream());
        //验证是否有失败的数据
        try {
            if (result.isVerifyFail()) {
                ServletOutputStream fos = response.getOutputStream();
//                //mime类型
//                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//                response.setHeader("Content-disposition", "attachment;filename=error.xlsx");
//                result.getFailWorkbook().write(fos);
//                fos.close();

                map.put("ER", failList);
                map.put("ee", "重复数据");
                map.get("ER");
                map.get("ee");
                return map;
            }
        } catch (Exception e) {
            map.put("ER", "解析文件失败");
            map.get("ER");
            return map;

        }
//        maintainMapper.addMaintain(list);
        map.put("ER", list);
        map.get("ER");
        map.put("ee", "添加成功");
        return map;
    }


    /**
     * 添加个体
     * @param maintainList
     * @return
     */

    @Override
    public Integer addMaintainSSS(List<MaintainDto> maintainList) {
        return maintainMapper.addMaintain(maintainList);
    }

    @Override
    public Integer addMaintainSSSser(List<MaintainDto> maintainList) {
        return maintainMapper.addMaintainSSSser(maintainList);
    }


    /**
     * 添加企业
     * @param maintainList
     * @return
     */
    @Override
    public Integer addMaintainaddMaintain(MaintainDtoList maintainList) {

        return maintainMapper.addMaintainMaintainDto(maintainList.getMaintainDtoList());
    }

    @Override
    public List<Maintain> queryList() {
        return maintainMapper.queryList();
    }
}
